package cn.echoedu.model;

import java.sql.SQLException;
import java.util.List;

import cn.echoedu.daoconstants.TableName;
import cn.echoedu.entity.AClass;
import cn.echoedu.entity.Department;
import cn.echoedu.entity.ReceiveSchoolNotify;
import cn.echoedu.entity.SchoolNotify;
import cn.echoedu.entity.User;
import cn.echoedu.entity.receivecommand.SchoolNotifyFirstReceive;
import cn.echoedu.entity.receivecommand.SchoolNotifyListReceive;
import cn.echoedu.entity.receivecommand.TeacherAddNotifyReceive;
import cn.echoedu.util.L;

public class ReceiveSchoolNotifyModel extends BaseModel{
	private final String T = "ReceiveSchoolNotifyModel------------>";
	/**
	 * Ĭ�ϵĲ���
	 * @param teacheraddnotifyreceive
	 * @param key
	 * @return
	 */
	public boolean putInfoWhenDefault(TeacherAddNotifyReceive 
			teacheraddnotifyreceive,int key) {
		// TODO Auto-generated method stub
		ReceiveSchoolNotify receiveschoolnotify = new ReceiveSchoolNotify();
		SchoolNotify schoolnotify = new SchoolNotify();
		schoolnotify.setSchoolnotifyid(key);
		receiveschoolnotify.setSchoolnotify(schoolnotify);
		Department department = new Department();
		department.setDepartmentcode(teacheraddnotifyreceive.getDepartmentcode());
		receiveschoolnotify.setDepartment(department);
		receiveschoolnotify.setSchoolnotifyisdefault(Integer.parseInt(
				teacheraddnotifyreceive.getIsdefault()));
		try {
			return getDAOInsert().putInto(getConnection(), 
					TableName.RECEIVE_SCHOOL_NOTIFY_TABLE,
					receiveschoolnotify,true);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
	/**
	 * ��Ĭ�ϵĲ��ò���ϵ��
	 * @param teacheraddnotifyreceive
	 * @param primarykey
	 */
	public boolean putInfoWhenIsNotDefault(
			TeacherAddNotifyReceive teacheraddnotifyreceive, int primarykey) {
		// TODO Auto-generated method stub
		SchoolNotify schoolnotify = new SchoolNotify();
		Department department = new Department();
		department.setDepartmentcode(teacheraddnotifyreceive.getDepartmentcode());
		schoolnotify.setSchoolnotifyid(primarykey);
		for(int userindex=0,classindex=0;
				isLess(userindex,teacheraddnotifyreceive.getReceive_person_list().size())
				||isLess(classindex,teacheraddnotifyreceive.getReceive_class_list().size());
				userindex++,classindex++){
			ReceiveSchoolNotify receiveschoolnotify = new ReceiveSchoolNotify();
			receiveschoolnotify.setSchoolnotify(schoolnotify);
			receiveschoolnotify.setSchoolnotifyisdefault(1);
            receiveschoolnotify.setDepartment(department);
			AClass aclass = new AClass();
			int classcode = teacheraddnotifyreceive.getReceive_class_list()
					.get(classindex).getClasscode();
			if(classcode!=0){
				aclass.setClasscode(classcode);
			}
			receiveschoolnotify.setAclass(aclass);

			User user = new User();
			String usernumber = teacheraddnotifyreceive.getReceive_person_list()
					.get(userindex).getUser_number();
			if(usernumber!=null){
				user.setUsernumber(usernumber);
			}
			receiveschoolnotify.setUser(user);
			try {
				if(!getDAOInsert().putInto(getConnection(), 
						TableName.RECEIVE_SCHOOL_NOTIFY_TABLE,
						receiveschoolnotify,true)){
					return false;
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				return false;
			}
		}
		return true;
	}
	private boolean isLess(int index,int size){
		return index<size?true:false;
	}
	public  List<ReceiveSchoolNotify> whenStudentQuery(SchoolNotifyListReceive schoolnotifyreceive) {
		// TODO Auto-generated method stub
		 final String M = "whenStudentQuery************>>";
		List<ReceiveSchoolNotify> receiveschoolnotifylist = null;
		String sql = "select * from receiveschoolnotify where receiveusernumber='"
				+schoolnotifyreceive.getUser_number()+"' or receivedepartmentcode='"
				+schoolnotifyreceive.getDepartmentcode()+"' or receiveclasscode='"
				+schoolnotifyreceive.getClasscode()+"' order by receivenotifyid desc limit "
				+(schoolnotifyreceive.getEndindex()-schoolnotifyreceive.getBeginindex());
		L.p(T+M+sql);
		try {
		    receiveschoolnotifylist = getDAORead().getPojoForSql(getConnection(), TableName.RECEIVE_SCHOOL_NOTIFY_TABLE,
					sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return receiveschoolnotifylist;
	}
	public List<ReceiveSchoolNotify> whenTeacherQuery(SchoolNotifyListReceive schoolnotifyreceive) {
		// TODO Auto-generated method stub
		final String M = "whenTeacherQuery********>>";
		String sql = "select * from receiveschoolnotify where receiveusernumber='"
				 +schoolnotifyreceive.getUser_number()+"' or receivedepartmentcode='"
				 +schoolnotifyreceive.getDepartmentcode()
				 +"' order by receivenotifyid desc limit "
				 +(schoolnotifyreceive.getEndindex()-schoolnotifyreceive.getBeginindex());
		List<ReceiveSchoolNotify> receiveschoolnotifylist=null;
		L.p(T+M+sql);
		try {
			 receiveschoolnotifylist = getDAORead().getPojoForSql(getConnection(), TableName.RECEIVE_SCHOOL_NOTIFY_TABLE,
					  sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		  return receiveschoolnotifylist;
	}
	public boolean deleteReceiveSchoolNotifyBySchoolNotifyid(int schoolnotifyid) {
		// TODO Auto-generated method stub
		return getDAODelete().deleteByColumnFrom(getConnection(),
				TableName.RECEIVE_SCHOOL_NOTIFY_TABLE,"receivenotifyid",
				schoolnotifyid+"");
	}
	public SchoolNotify querySchoolNotifyFirst(
			SchoolNotifyFirstReceive schoolnotifyfirst) {
		// TODO Auto-generated method stub
		String sql = getSql(schoolnotifyfirst);
		try {
			List<SchoolNotify> list= getDAORead().getPojoForSql(getConnection(),
					TableName.SCHOOL_NOTIFY_TABLE, sql);
		   if(list!=null&&list.size()>0){
			   return list.get(0);
		   }
		   
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	private String getSql(SchoolNotifyFirstReceive schoolnotifyfirst) {
		// TODO Auto-generated method stub
		StringBuilder sqlstr = new StringBuilder();
		sqlstr.append("select * from receiveschoolnotify right join " +
				"schoolnotify on receiveschoolnotify.receivenotifyid" +
				"= schoolnotify.schoolnotifyid where receivenotifyid in("+
				"select max(receivenotifyid) from receiveschoolnotify where"+ 
				"(receiveusernumber =1" +
				"  or receiveclasscode = ");
		sqlstr.append(schoolnotifyfirst.getAclasscode());
		sqlstr.append(" or receivedepartmentcode =  ");
		sqlstr.append(schoolnotifyfirst.getDepartmentcode());
		sqlstr.append(" and schoolnotifyisdefault = 1");
		sqlstr.append(") or (receivedepartmentcode=");
		sqlstr.append(schoolnotifyfirst.getDepartmentcode());
		sqlstr.append(" and schoolnotifyisdefault=0))");
		return sqlstr.toString();
	}
}
